#!/bin/bash

# FFmpeg iOS编译脚本
# 使用方法: ./build_ffmpeg_ios.sh

set -e

# 配置变量
FFMPEG_VERSION="6.0"
BUILD_DIR="./ffmpeg_build"
OUTPUT_DIR="./ios/FFmpeg"

# 创建构建目录
mkdir -p $BUILD_DIR
cd $BUILD_DIR

# 下载FFmpeg源码
if [ ! -d "ffmpeg-$FFMPEG_VERSION" ]; then
    echo "下载FFmpeg源码..."
    wget https://ffmpeg.org/releases/ffmpeg-$FFMPEG_VERSION.tar.bz2
    tar -xjf ffmpeg-$FFMPEG_VERSION.tar.bz2
fi

cd ffmpeg-$FFMPEG_VERSION

# 编译函数
build_ffmpeg_ios() {
    local ARCH=$1
    local SDK=$2
    local PREFIX=$3
    local CC=$4
    local CFLAGS=$5
    
    echo "编译 $ARCH 架构..."
    
    ./configure \
        --prefix=$PREFIX \
        --arch=$ARCH \
        --target-os=darwin \
        --enable-cross-compile \
        --cc=$CC \
        --extra-cflags="$CFLAGS" \
        --extra-ldflags="$CFLAGS" \
        --enable-shared \
        --disable-static \
        --enable-gpl \
        --enable-version3 \
        --disable-doc \
        --disable-programs \
        --disable-ffmpeg \
        --disable-ffplay \
        --disable-ffprobe \
        --disable-avdevice \
        --disable-avfilter \
        --disable-postproc \
        --disable-encoders \
        --disable-decoders \
        --enable-decoder=h264 \
        --enable-decoder=h265 \
        --enable-decoder=vp8 \
        --enable-decoder=vp9 \
        --enable-decoder=aac \
        --enable-decoder=mp3 \
        --enable-decoder=opus \
        --enable-decoder=vorbis \
        --enable-decoder=flac \
        --enable-decoder=wav \
        --enable-decoder=pcm \
        --disable-network \
        --disable-protocols \
        --disable-filters \
        --disable-hwaccels \
        --disable-parsers \
        --disable-bsfs \
        --disable-indevs \
        --disable-outdevs \
        --disable-devices \
        --disable-debug \
        --disable-ffplay \
        --disable-ffprobe \
        --disable-doc \
        --disable-manpages \
        --disable-podpages \
        --disable-txtpages \
        --disable-htmlpages

    make clean
    make -j$(nproc)
    make install
}

# 获取iOS SDK路径
IOS_SDK_PATH=$(xcrun --sdk iphoneos --show-sdk-path)
IOS_SIM_SDK_PATH=$(xcrun --sdk iphonesimulator --show-sdk-path)

echo "iOS SDK路径: $IOS_SDK_PATH"
echo "iOS Simulator SDK路径: $IOS_SIM_SDK_PATH"

# 编译ARM64 (真机)
build_ffmpeg_ios \
    "arm64" \
    "iphoneos" \
    "$BUILD_DIR/ios/arm64" \
    "clang" \
    "-arch arm64 -isysroot $IOS_SDK_PATH -mios-version-min=11.0"

# 编译x86_64 (模拟器)
build_ffmpeg_ios \
    "x86_64" \
    "iphonesimulator" \
    "$BUILD_DIR/ios/x86_64" \
    "clang" \
    "-arch x86_64 -isysroot $IOS_SIM_SDK_PATH -mios-version-min=11.0"

# 创建通用库
echo "创建通用库..."
mkdir -p $BUILD_DIR/ios/universal/lib
mkdir -p $BUILD_DIR/ios/universal/include

# 复制头文件
cp -r $BUILD_DIR/ios/arm64/include/* $BUILD_DIR/ios/universal/include/

# 创建通用库文件
for lib in libavcodec libavformat libavutil libswscale libswresample; do
    echo "创建通用库: $lib"
    lipo -create \
        $BUILD_DIR/ios/arm64/lib/$lib.a \
        $BUILD_DIR/ios/x86_64/lib/$lib.a \
        -output $BUILD_DIR/ios/universal/lib/$lib.a
done

# 复制库文件到项目目录
echo "复制库文件到项目目录..."
mkdir -p $OUTPUT_DIR/lib
mkdir -p $OUTPUT_DIR/include

cp $BUILD_DIR/ios/universal/lib/*.a $OUTPUT_DIR/lib/
cp -r $BUILD_DIR/ios/universal/include/* $OUTPUT_DIR/include/

echo "FFmpeg iOS编译完成！"
echo "库文件位置: $OUTPUT_DIR/lib"
echo "头文件位置: $OUTPUT_DIR/include"
